import { Module, Scope } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Event } from 'src/events/entities/event.entity';
import coffeesConfig from './coffees.config';
import { COFFEES_BRANDS } from './coffees.constants';
import { CoffeesController } from './coffees.controller';
import { CoffeesService } from './coffees.service';
import { Coffee } from './entities/coffee.entity';
import { Flavor } from './entities/flavor.entity';

@Module({
  imports: [
    // 注入实例
    TypeOrmModule.forFeature([Coffee, Flavor, Event]),
    // 注入配置
    ConfigModule.forFeature(coffeesConfig),
  ],
  controllers: [CoffeesController],
  providers: [
    CoffeesService,
    {
      provide: COFFEES_BRANDS,
      useFactory: async () => {
        const coffeeBrands = await Promise.resolve(['buddy brew', 'nescafe']);
        console.log('coffeeBrands Factory RUN');
        return coffeeBrands;
      },
      // scope: Scope.TRANSIENT,
    },
    {
      provide: coffeesConfig.KEY,
      useFactory: (configService: ConfigService) => {
        return configService.get('coffees');
      },
      inject: [ConfigService],
    },
  ],
  exports: [CoffeesService],
})
export class CoffeesModule {}
